attribute vec3 inPosition;
attribute vec3 inNormal;
attribute vec3 inTangent;
attribute vec2 inTex2dCoord;
uniform mat4 inProjMat;
uniform mat4 inViewMat;
uniform mat4 inModelMat;
uniform vec3 inDiffuseCoefficient;//漫反射系数(材质反射光的颜色)
uniform vec3 inSpecularCoefficient;//镜面反射系数
uniform vec3 inAmbientCoefficient;//环境光系数
struct light
{
	int type;
	vec3 light;//光源坐标
	float intensity;//光强
};
const int LIGHT_MAX = 4;
uniform light inLight[LIGHT_MAX];
varying vec2 outTexCoord;
varying mat3 outTBN;
varying vec3 outLightDir[LIGHT_MAX];
varying vec3 outHalf[LIGHT_MAX];
varying float outDis[LIGHT_MAX];

void main()
{
	outTexCoord = inTex2dCoord;
	mat3 rot = mat3(inModelMat);
	vec4 position = inModelMat*vec4(inPosition, 1.0);
	// outNormal = rot*inTangent;
	gl_Position = inProjMat*inViewMat*position;
	vec3 camera = vec3(inViewMat[0][3],inViewMat[1][3],inViewMat[2][3]);//像机坐标
	vec3 v = normalize(camera-vec3(position));//像机向量
	for (int i=0; i<LIGHT_MAX; ++i)
	{
		if(inLight[i].type==1)//点光源
		{
			outLightDir[i] = normalize(inLight[i].light-vec3(position));//光线向量
			outHalf[i] = normalize(v+outLightDir[i]);//半程向量
			outDis[i] = distance(inLight[i].light,vec3(position));//光源到顶点的距离
		}
	}
	vec3 T = normalize(rot * inTangent);
	vec3 N = normalize(rot * inNormal);
	vec3 B = cross(T,N);
	outTBN = transpose(mat3(T, B, N));
}